package xyz.zyt123.hikvideo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Autowired
  private VideoConfig videoConfig;

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry
        .addMapping("/**")
        .maxAge(3600)
        .allowCredentials(true)
        .allowedOriginPatterns("*")
        .allowedMethods("*")
        .allowedHeaders("*")
        .exposedHeaders("token", "Authorization")
    ;
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/api/live/**").addResourceLocations("file:/" + videoConfig.getPath());
    registry.addResourceHandler("/api/image/**").addResourceLocations("file:/" + videoConfig.getPicture());
    registry.addResourceHandler("/api/video/**").addResourceLocations("file:/" + videoConfig.getVideo());
  }
}
